/**
 * @description: 默认配置项
 * @version: 0.0.1
 * @author: xiech
 * @Date: 2023-06-12 18:16:39
 */

import { MidwayConfig } from '@midwayjs/core';
import { configOptions } from '../interface';
// import { uploadWhiteList } from '@midwayjs/upload';
import * as fs from 'fs';
import { resolve, join } from 'path';

export default (appInfo: any): MidwayConfig => {
  const config: configOptions = {};
  // console.log(appInfo);

  // key
  config.keys = '1685503993697_3810';
  config.keywords = '本地';
  config.koa = {
    port: 7001,
  };

  // 中间键配置
  config.middleware = {
    format: {
      prefix: '/api', // 指定已 /api开头的接口地址需要拦截
      // ignore: ['/api/v1/admin/working/downloadAdminWorkingFile'], // 指定该接口地址，不需要拦截
      ignore: []
    },
    jwt: {
      prefix: '/api', // 指定已 /api开头的接口地址需要拦截
      // ignore: ['/api/v1/login/getRedirectUri', '/api/v1/login/getUserInfo', '/api/v1/auth/getUri', '/api/v1/auth/accessToken', '/api/v1/auth/userData'], // 指定该接口地址，不需要拦截
      ignore: []
    },
    auth: {
      prefix: '/api', // 指定已 /api开头的接口地址需要拦截
      // ignore: ['/api/v1/login/getRedirectUri', '/api/v1/login/getUserInfo', '/api/v1/auth/getUri', '/api/v1/auth/accessToken', '/api/v1/auth/userData'], // 指定该接口地址，不需要拦截
      ignore: []
    }
  };

  // @midwayjs/jwt koa config 配置
  config.jwt = {
    // secret: 'setscrew',
    secret: fs.readFileSync(resolve(__dirname, '../config/keys/hs256.key'), 'utf-8'),
    // expiresIn: 10, // 有效期测试
    expiresIn: 60 * 60 * 2, // 有效期
  };

  // 钉钉报警配置
  config.dingTalk = {
    webhook: 'https://oapi.dingtalk.com/robot/send?access_token=8a2b489f9b4314f3d3a7a80bf8da2c077a14ffbdb48d83348de4a85588bc956b',
    signSEC: 'SECce1c223ff459fb8e14f122877757e6a3be9a070161edcb1403c52cc7bc2f6f52',
  };

  // 任务列表
  config.bull = {
    defaultQueueOptions: {
      // redis: {
      //   port: 6379, // Redis port
      //   host: '192.168.0.233', // Redis host
      //   password: '',
      //   db: 1, 
      // },
      redis: {
        port: 6379, // Redis port
        host: '127.0.0.1', // Redis host
        password: '',
        db: 1,
      },
    },
  };

  // 跨域
  config.cors = {
    origin: '*',
    allowMethods: 'GET,HEAD,PUT,POST,DELETE,PATCH',
    credentials: true,
  };

  // 本地
  config.sequelize = {
    dataSource: {
      default: {
        dialect: 'mysql',
        host: '127.0.0.1', // 数据库IP
        port: 3306, // 数据库端口
        username: 'root', // 数据库用户名（需要有创建表结构权限）
        password: 'root', // 数据库密码
        database: 'morefun_oa', // 数据库IP
        encrypt: false,
        define: { charset: 'utf8' },
        timezone: '+08:00',
        // 或者扫描形式
        entities: ['**/entity/*.entity{.ts,.js}'],
        sync: false, // 本地的时候，可以通过 sync: true 直接 createTable
        repositoryMode: true,
      },
    },
  };

  // 缓存 Redis
  config.redis = {
    client: {
      port: 6379, // Redis port
      // host: '192.168.0.233', // Redis host
      host: '127.0.0.1', // Redis host
      password: '',
      db: 0,
    },
  };

  // 存储 oss
  config.oss = {
    client: {
      accessKeyId: 'LTAI5t98JjBQAWqKPqGppwRT',
      accessKeySecret: 'zqwPi65P4j7KmNZQF7HsH6kaBDjkPo',
      bucket: 'morefun-active',
      endpoint: 'oss-cn-beijing.aliyuncs.com',
      timeout: '60s',
    },
  };

  //
  const tmpdir = join(appInfo.appDir, 'tempfile');
  config.tempfile = tmpdir;

  // 常见上传文件
  const imageFormats = ['.jpg', '.jpeg', '.png', '.gif', '.bmp', '.svg'];
  const videoFormats = ['.mp4', '.mov', '.avi', '.wmv', '.flv'];
  const audioFormats = ['.mp3', '.wav', '.m4a', '.flac'];
  const textFormats = ['.txt', '.csv', '.json', '.xml', '.md'];
  const documentFormats = ['.doc', '.docx', '.xls', '.xlsx', '.ppt', '.pptx', '.pdf'];
  const compressedFormats = ['.zip', '.rar', '.tar.gz', '.7z'];
  const fontFormats = ['.ttf', '.otf', '.woff', '.woff2'];
  const codeFormats = ['.html', '.css', '.js', '.php', '.java', '.py'];
  const databaseFormats = ['.sql', '.sqlite', '.dbf', '.mdb'];
  const logFormats = ['.log', '.txt', '.csv'];

  const commonFileFormats = [
    ...imageFormats,
    ...videoFormats,
    ...audioFormats,
    ...textFormats,
    ...documentFormats,
    ...compressedFormats,
    ...fontFormats,
    ...codeFormats,
    ...databaseFormats,
    ...logFormats,
  ];

  // 上传配置
  config.upload = {
    mode: 'stream', // mode: UploadMode, 默认为file，即上传到服务器临时目录，可以配置为 stream
    fileSize: '10mb', // fileSize: string, 最大上传文件大小，默认为 10mb
    // whitelist: uploadWhiteList.filter(ext => ext !== '.pdf'),
    whitelist: commonFileFormats, // whitelist: string[]，文件扩展名白名单
    tmpdir:tmpdir, // tmpdir: string，上传的文件临时存储路径
    // cleanTimeout: 5 * 60 * 1000, // cleanTimeout: number，上传的文件在临时目录中多久之后自动删除，默认为 5 分钟
    cleanTimeout: 1 * 60 * 1000, // cleanTimeout: number，上传的文件在临时目录中多久之后自动删除，默认为 5 分钟
    base64: false, // base64: boolean，设置原始body是否是base64格式，默认为false，一般用于腾讯云的兼容
    // match: /\/api\/file\/upload/, // 仅在匹配路径到 /api/upload 的时候去解析 body 中的文件信息
  };

  config.staticFile = {
    dirs: {
      default: {
        prefix: '/',
        dir: join(appInfo.appDir, 'html'),
        alias: {
          '/': '/index.html',
        },
      },
    }
  };

  //  钉钉登录回调地址
  config.dingtalkAuthConfig = {
    redirect_uri : 'https://morefun-oatest.morefun.me/login',
    login_uri : 'https://login.dingtalk.com/oauth2/auth',
    oapi_uri: 'https://oapi.dingtalk.com',
    api_uri: 'https://api.dingtalk.com',
    mf_auth_base: 'https://morefun-oatest.morefun.me/mf-auth/',
    oauth_path: 'https://morefun-oa.morefun.me/mf-oauth'
  }
  //  钉钉应用配置信息
  config.appConfig = {
    appName: "莫凡OA-H5",
    appKey: "dingsdfd0sfotgj0og95",
    appSecret: "GiZ391DfZ647YTQiSdSokpj9SzVG6DtfqlaOcAQUU5JqgRKlkkg0dS3bkcTeAQtx"
  }

  return config;
};
